/*
 * Have to change racetrack.setInterval to number of intervals between control
 * points. Not how it is atm. Atm it is messy as the last interval is not spaced
 * correctly relative to the first interval of the next span.
 * 
 * Also have to look at track edge interval calculation. The inner edge on corners
 * needs to not loop over itself.
 */
package sc;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

public final class Main extends JFrame implements MouseListener, MouseMotionListener, KeyListener {

    private static final long serialVersionUID = 1L;
    private Racetrack2D track;
    private long[] framerate;
    private boolean stop = false;
    private int width;
    private int height;
    private BufferedImage bufimg;
    private Graphics buffer;
    private int selection;
    private boolean trackUpdate;

    public Main(int width, int height) {
    	this.width = width;
    	this.height = height;
    	
        setTitle("Racetrack");
    	setResizable(false);
    	setSize(width, height);
    	setLocation(100, 300);
    	
        addMouseListener(this);
        addMouseMotionListener(this);
        addKeyListener(this);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        track = new Racetrack2D();
        track.setWidth(50);
        
        framerate = new long[5];
        framerate[4] = 100;
        
        bufimg = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        buffer = bufimg.getGraphics();
        
        selection = -1;
        trackUpdate = false;
        
        setVisible(true);
    }
    
    public void run() {
    	Color black = new Color(0, 0, 0);
    	Color white = new Color(255, 255, 255);
    	TPS tps = new TPS(1);
    	Racetrack2DGenerator generate = new Racetrack2DGenerator(10, 30, this.width - 20, this.height - 20);
    	
        // Main loop repeats while JFrame is visible
        while (isVisible()) {
        	if (tps.tick()) {
        		// Skip generating a new track if stopped
        		if (stop) {
        			continue;
        		}
        		
                // Get the off screen graphics context
                buffer.setColor(white);
                buffer.fillRect(0, 0, width, height);

                if (!trackUpdate) {
                	trackUpdate = true;
                	track = generate.create(buffer);
                	track.drawtrack(buffer);
                	trackUpdate = false;
                }
                
                // Draw stats
                buffer.setColor(black);
                buffer.drawString(tps.avgTPS() + " tps " + tps.skipTime() + "/" + tps.spareTime() + " selection: " + selection, 5, 40);
                
                getGraphics().drawImage(bufimg, 0, 0, null);
            }
        }
    }

    public void mouseClicked(MouseEvent e) {
    	if (selection == -1) {
    		if (!trackUpdate) {
    			trackUpdate = true;
    			track.appendControlPoint(e.getX(), e.getY());
    			track.update();
    			trackUpdate = false;
    		}
    	}
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    	selection = track.getSpline().near(e.getX(), e.getY(), 5);
    }

    public void mouseReleased(MouseEvent e) {
    	selection = -1;
    }

	public void mouseDragged(MouseEvent e) {		
	}

	public void mouseMoved(MouseEvent e) {
	}
	
	public void keyPressed(KeyEvent e) {
	}

	public void keyReleased(KeyEvent e) {
		switch (e.getKeyCode()) {
			case KeyEvent.VK_ESCAPE:
				System.exit(0);
		}
	}

	public void keyTyped(KeyEvent e) {
	}

    public static void main(String[] args) {
        Main b = new Main(800, 600);
        b.run();
    }
    
}
